package application.gui.adminWindow;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.Admin;
import application.LoginAndAdministrationModule.PersonData;
import application.client.data.Client;
import application.parcelModule.Parcel;

public class ParcelDetails extends JFrame {
	public ParcelDetails(AdminHomePage parent, Communication c, PersonData p,
			Parcel parcel) {
		this.parent = parent;
		this.db = c;
		this.admin = new Admin(p);
		this.parcel = parcel;
		setSize(480, 320);
		setLayout(null);
		setResizable(false);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		initTable();
		initComponents();
	}

	private void initComponents() {
		buttonPanel = new JPanel(null);
		buttonPanel.setSize(480, 50);
		close = new JButton("Zamknij");
		buttonPanel.add(close);
		close.setBounds(300, 0, 100, 40);
		deleteParcel = new JButton("Usuń");
		buttonPanel.add(deleteParcel);
		deleteParcel.setBounds(0, 0, 150, 40);
		add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setBounds(0, 200, 480, 50);
		close.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				parent.setVisible(true);
				dispose();
			}
		});
		deleteParcel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					if (db.deleteParcel(parcel.getParcelID())) {
						parent.setVisible(true);
						parent.getDeleteParcel().doClick();
						dispose();
					}
				} catch (SQLException e1) {
					
					e1.printStackTrace();
				}
			}
		});
		changeRecipient = new JButton("Zmień adresata");
		buttonPanel.add(changeRecipient);
		changeRecipient.setBounds(150, 0, 150, 40);
		if (parcel.getStatus() != 1 && parcel.getStatus() != 2) {
			changeRecipient.setVisible(false);
		} else {
			changeRecipient.setVisible(true);
		}
		changeRecipient.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				parent.registerMsg(2);
				try {
					parent.getNewParcelPanel().editParcelMode(
							db.getClientsByField("id",
									String.valueOf(parcel.getRecipient())).get(
									0), parcel);
					dispose();
				} catch (SQLException e1) {
					
					e1.printStackTrace();
				}
			}
		});
	}

	public void initTable() {
		// TODO Auto-generated method stub
		LinkedList<String[]> list = new LinkedList<String[]>();
		Client recipien;
		PersonData sender;
		try {
			recipien = db.getClientsByField("id",
					String.valueOf(parcel.getRecipient())).get(0);
			sender = admin.getUserById(parcel.getSender());
			String[] row = { "ID paczki", String.valueOf(parcel.getParcelID()) };
			list.add(row);
			row = new String[] { "Imię i nazwisko nadawcy",
					sender.getName() + " " + sender.getSurname() };
			list.add(row);
			row = new String[] { "Data nadania", parcel.getDate() };
			list.add(row);
			row = new String[] {
					"Wymiary",
					String.valueOf(parcel.getWidth()) + "x"
							+ String.valueOf(parcel.getHeight()) + "x"
							+ String.valueOf(parcel.getLength()) + " - "
							+ String.valueOf(parcel.getMass()) + "dag" };
			list.add(row);
			row = new String[] { "Cena", String.valueOf(parcel.getPrice()) };
			list.add(row);
			row = new String[] { "Imię i nazwisko odbiorcy",
					recipien.getFirstName() + " " + recipien.getLastName() };
			list.add(row);
			row = new String[] {
					"Adres",
					recipien.getAddress().getStreet() + ", "
							+ recipien.getAddress().getFlatNumber() + "/ "
							+ recipien.getAddress().getHouseNumber() };
			list.add(row);
			row = new String[] { "Kod pocztowy",
					recipien.getAddress().getPostCode() };
			list.add(row);
			row = new String[] { "Miejscowość", recipien.getAddress().getCity() };
			list.add(row);
			row = new String[] { "Status dostarczenia",
					db.getParcelStatus(parcel.getParcelID()) };
			list.add(row);
			data = list.toArray(new String[2][list.size()]);
		} catch (SQLException e1) {
			
			e1.printStackTrace();
		}

		table = new JTable(data, names);
		add(table);
		table.setBounds(0, 0, 480, 200);

	}

	private AdminHomePage parent;
	private Parcel parcel;
	private JTable table;
	private String[] names = { "", "" };
	private String[][] data;
	private JButton close;
	private JButton deleteParcel;
	private JButton changeRecipient;
	private JPanel buttonPanel;
	private Admin admin;
	private Communication db;

}
